package com.sdf.com.emjdemo;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements DefaultEmoGridView.OnEmoItemClickListener, ViewPager.OnPageChangeListener {
    private ViewPager emotionVp;
    private EditText chatEdit;
    private PagerIndicatorView piv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        emotionVp = (ViewPager) findViewById(R.id.emotion_vp);
        emotionVp.setAdapter(new EmotionAdapter());
        emotionVp.addOnPageChangeListener(this);
        chatEdit = (EditText) findViewById(R.id.chat_edit);
        //页面的指示器
        piv = (PagerIndicatorView) findViewById(R.id.piv);
        piv.initData(5, 0);
    }

    @Override
    public void onEmoItemClick(int resId, String resName) {
        //点击到的表情
        //加载表情图片
        Drawable drawable = getResources().getDrawable(resId);
        //创建图文混排的string类
        resName = "[" + resName + "]";
        SpannableString ss = new SpannableString(resName);
        //设置drawable的大小
        int size = (int) (getResources().getDisplayMetrics().density * 25);
        drawable.setBounds(0, 0, size, size);
        ImageSpan imageSpan = new ImageSpan(drawable);
        ss.setSpan(imageSpan, 0, resName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //获取光标的位置
        int selectionEnd = chatEdit.getSelectionEnd();
        //获取当前editText中现在的内容
        Editable originalText = chatEdit.getText();
        if (selectionEnd < originalText.length()) {
            //光标在文字的中间
            originalText.insert(selectionEnd, ss);
        } else {
            originalText.append(ss);
        }
    }

    @Override
    public void onDelete() {
        //删除EditText中的内容
        //模拟系统的删除键
        chatEdit.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
        chatEdit.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //切换页码指示器
        piv.notifiyDataSetChanged(5, position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //表情的适配器
    class EmotionAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //创建显示的页面
            DefaultEmoGridView defaultEmoGridView = new DefaultEmoGridView(container.getContext());
            //初始化表情
            defaultEmoGridView.initData(5, position);
            //设置点击事件
            defaultEmoGridView.setOnEmoItemClickListener(MainActivity.this);
            container.addView(defaultEmoGridView);
            return defaultEmoGridView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
